package com.wordpress.music2dot0.server;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Represents a list of group servers
 * 
 * @author raf
 */
@XmlRootElement(name = "data")
public class GroupServerList {
	@XmlElement
	List<GroupServer> group = new ArrayList<GroupServer>();

	/**
	 * Adds an existing group to the list
	 * 
	 * @param newGroup
	 *            the group to add
	 */
	public void add(GroupServer newGroup) {
		this.group.add(newGroup);
	}

	/**
	 * Retrieves a group by name
	 * 
	 * @param name
	 *            the name of the group to find
	 * @return the group with the given name, or null if nothing is found
	 */
	public GroupServer get(String name) {
		for (final GroupServer currentGroup : this.group) {
			if (name.equals(currentGroup.getName())) {
				return currentGroup;
			}
		}
		return null;
	}

	/**
	 * Finds the groups that the client is in the radius of
	 * 
	 * @param latitude
	 *            the latitude to search for within the radius
	 * @param longitude
	 *            the longitude to search for within the radius
	 * @return the list of groups that the client is in the radius of
	 */
	public GroupServerList near(double latitude, double longitude) {
		final GroupServerList groupsNear = new GroupServerList();
		for (final GroupServer currentGroup : this.group) {
			if (currentGroup.isNear(latitude, longitude)) {
				groupsNear.add(currentGroup);
			}
		}
		return groupsNear;
	}
}
